
// const drawTool = {

// }

import { loadModules } from 'esri-loader'
import Geometry from './geometry'
import config from './config'

let Drawing;

function DrawTool(map, loadSucc){

    this.map = map;
    this.succFun = null;
    loadModules(['esri/toolbars/draw'])
    .then(([Draw]) => {
        Drawing = Draw;
        this.toolbar = new Draw(this.map);

        this.toolbar.on('draw-end',this.onDrawEnd.bind(this));

        if(loadSucc && typeof loadSucc=='function'){
            loadSucc();
        }
    })

}

DrawTool.prototype.onDrawEnd = function(geometry){
    
    this.toolbar.deactivate();
    loadModules(['esri/graphic','esri/symbols/SimpleMarkerSymbol','esri/symbols/SimpleLineSymbol','esri/symbols/SimpleFillSymbol'])
    .then(([Graphic, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol]) => {
        let graphic = new Graphic(geometry.geometry, 
            Geometry.getDefaultSymbol(geometry.geometry, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol));
        if(this.succFun && typeof this.succFun=='function'){
            this.succFun(geometry.geometry, graphic);
            this.succFun = null;
        }else{
            this.map.graphics.add(graphic);
        }
    })
}

DrawTool.prototype.drawPoint = function(fun){
    this.succFun = fun;
    this.toolbar.activate(Drawing.POINT);
}

DrawTool.prototype.drawPolyline = function(fun){
    this.succFun = fun;
    this.toolbar.activate(Drawing.POLYLINE);
}

DrawTool.prototype.drawPolygon = function(fun){
    this.succFun = fun;
    this.toolbar.activate(Drawing.POLYGON);
}

DrawTool.prototype.drawGeo = function(type, fun){
    switch(type) {
        case config.toolType.POINT:
            this.drawPoint(fun);
            break;
        case config.toolType.POLYLINE:
            this.drawPolyline(fun);
            break;
        case config.toolType.POLYGON:
            this.drawPolygon(fun);
            break;
        default: 
            break;
    }
}

export default DrawTool;